package com.fram.controller.api;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.fram.bean.api.volume.request.VolumeAnswerRequest;
import com.fram.bean.sys.ResponseMsg;
import com.fram.common.canstant.ErrCode;
import com.fram.common.exception.ProAffairException;
import com.fram.common.exception.ProException;
import com.fram.common.util.PublicMethod;
import com.fram.common.util.apiversion.ApiVersion;
import com.fram.common.util.base.BaseController;
import com.fram.service.api.v1.VolumeService;

/**前台 调查问卷展示以及增加**/
@RestController
@RequestMapping("/api/{version}/volume")
public class VolumeController extends BaseController {
	private static Logger LOG = LoggerFactory.getLogger(VolumeController.class);

	@Autowired
	private VolumeService volumeService;
	
	/**展示 卷**/
	@ApiVersion(1.0)
	@RequestMapping("edition/list")
	private ResponseMsg editionList() {
		ResponseMsg msg =ResponseMsg.returnBeanBuild();
		try {
			msg.setData(volumeService.ListApiEdition());
		} catch (ProException e) {
			LOG.error("-（卷目）版本列表->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-（卷目）版本列表->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
	// 展示问题
	@ApiVersion(1.0)
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	private ResponseMsg list(String version) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			msg.setData(volumeService.getVolumePaperList(version,getApiUserId()));
		} catch (ProException e) {
			LOG.error("-展示所有问题->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-展示所有问题->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
	
	// 点击提交 （增加到 回答某某库）选择题有可能多选得问小松哥
	@ApiVersion(1.0)
	@RequestMapping(value = "add/answer", method = RequestMethod.POST)
	private ResponseMsg addAnswer(@RequestBody List<VolumeAnswerRequest> request) {
		ResponseMsg msg = ResponseMsg.returnBeanBuild();
		try {
			for (VolumeAnswerRequest req : request) {
				req.setUserId(getApiUserId());
			}
			msg.setData(volumeService.insertAnswer(request));
		} catch (ProAffairException e) {
			LOG.error("-点击提交问题->{},{},{}", msg.getSessionId(), e.getErrorCode(), e.getErrorMsg());
			msg.setReturnCode(e.getErrorCode()).setReturnMsg(e.getErrorMsg());
		} catch (Exception e) {
			LOG.error("-点击提交问题->{},{}", msg.getSessionId(), PublicMethod.printErrorTrace(e));
			msg.setReturnCode(ErrCode.ERR.getReturnCode()).setReturnMsg(ErrCode.ERR.getReturnMsg());
		}
		return msg;
	}
}
